using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using virtuaPol.Model;

namespace virtuaPol.vpTest
{
    class TestSerializeWorld : ITestCase
    {
        private int numIterations;
        private CWorld world;
        public CWorld World
        {
            get { return world; }
            set { world = value; }
        }

        private string report = "";
        
        public TestSerializeWorld(int iterations)
        {
            numIterations = iterations;
        }

        public void initialize()
        {
            CCountry c;
            CPerson p;
            CProperty pr;
            report += "Serialize World Object Test\n";
            world = new CWorld("Test World");
            for (int i = 1; i <= numIterations; i++)
            {
                c = new CCountry();
                c.Name = "Test Country " + i.ToString();
                for (int j = 1; j <= numIterations; j++)
                {
                    p = new CPerson();
                    p.Name = "Test Person " + j.ToString();
                    c.addPerson(p);
                }
                world.addCountry(c);

                pr = new CProperty("Property " + i.ToString(), CPropertyValueTypes.Integer, (i % 2 == 0 ? 1 : 0));
                world.Properties.Add(pr);
            }
            report += "World object initialized.\n";
        }

        public bool execute()
        {
            try
            {
                TextWriter xml = new StreamWriter("test.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(CWorld));
                serializer.Serialize(xml, world);
                xml.Close();
                report += "World object successfully serialized to test.xml.\n";
                return true;
            }
            catch (Exception e)
            {
                report += "Serialization failed! An error occurred.\n" + e.Message + "\n";
                return false;
            }
        }

        public void cleanup()
        { }

        public string getReport()
        {
            return report;
        }
    }
}
